package com.iursp.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iursp.domain.Const;
import com.iursp.domain.model.user.CommonUser;

public final  class UserUtils {

	private static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}

	/**
	 * 从session中获取用户
	 * 
	 * @param session
	 * @return user
	 */
	public static CommonUser getUser() {
		return (CommonUser) getSession().getAttribute(Const.SESSION_USER_KEY);
	}

	public static void setUser(CommonUser user) {
		getSession().setAttribute(Const.SESSION_USER_KEY, user);
	}

	public static void removeUser() {
		HttpSession session = getSession();
		session.removeAttribute(Const.SESSION_USER_KEY);
		session.invalidate();
	}
}
